package com.veo.dao.Impl;

import com.veo.dao.AdminDao;
import com.veo.entity.Admin;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("adminDao")
public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {

    @Resource(name = "sessionFactory")
    public void setSessionFactory1(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }



    @Override
    public List<Admin> getAllAdmin() {
        List<Admin> admins = this.getHibernateTemplate().loadAll(Admin.class);
        return admins;
    }

    @Override
    public void deleteAdmin(Admin admin) {
        this.getHibernateTemplate().delete(admin);
    }

    @Override
    public void addAdmin(Admin admin) {
        this.getHibernateTemplate().save(admin);
        this.getHibernateTemplate().flush();

    }

    @Override
    public void updateAdmin(Admin admin) {
        this.getHibernateTemplate().update(admin);
        this.getHibernateTemplate().flush();
    }

    @Override
    public Admin findLoginAdmin(Admin admin) {
        List<Admin> list = (List<Admin>) this.getHibernateTemplate().find("from Admin where username='" + admin.getUsername() + "' and password='" + admin.getPassword()+"'");
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean findUsername(Admin admin) {
        List<Admin> list = (List<Admin>) getHibernateTemplate().find("from Admin where username='" + admin.getUsername()+"'");
        if (list.size()>0){
            return true;
        }
        return false;
    }


}
